<template>
  <div class="fontDa">
<!--    <el-dialog :visible.sync="dialogVisible" fullscreen destroy-on-close append-to-body @close="cacel()"-->
<!--               :before-close="handleClose">-->
      <el-container>
        <el-main :style="mode=='hz'?'height:calc(100vh - 210px); padding: 0 45px;':'height:calc(100vh - '+logoHeight+'px); padding: 0 45px;'">
          <div v-if="mode=='df'">
            <cp-xp3 ref="cpXp3" v-if="cepingpiaoVisible3"></cp-xp3>
            <cp-xp4 ref="cpXp4" v-if="cepingpiaoVisible4" @get-mode="getModeEvent"></cp-xp4>
            <cp-xp5 ref="cpXp5" v-if="cepingpiaoVisible5"></cp-xp5>
          </div>
          <div v-if="mode=='hz'">
            <cp-hz-pz ref="cpHz" @openXp="openXp" ></cp-hz-pz>
          </div>
        </el-main>
      </el-container>
      <div class="fontDa" v-if="!hideBtn">
<!--      <div class="el-icon-back arrowBtn" @click="up()" v-if="dqwz>0&&mode=='df'"></div>-->
<!--      <div class="el-icon-right arrowBtn" @click="next()" v-if="mode!='hz' && (dqwz+1) < xpList.length"></div>-->
        <div class="el-icon-back arrowBtn" @click="up()" v-if="currentPage!=1"></div>
        <div class="el-icon-right arrowBtn" @click="next()" v-if="mode!='hz'"></div>
      </div>
      <div class="fontDa">
<!--      <div class="shuoMing" style="margin-left: 140px;" v-if="mode=='hz'">★点击“返回测评界面”按钮，可返回该张测评票进行测评分值修改。</div>-->
        <div class="flex justifyContentSpaceBetween font22" style="margin-left: 40px; margin-top:20px;" v-if="mode=='hz'">
          <div><!--★点击“返回测评界面”按钮，可返回该张测评票进行测评分值修改。--></div>
          <div class="flex justifyContentSpaceBetween " style="margin-right:60px;">
            <span><img src="/img/logo.png" width="40" height="40"></span><span class="ml10 font28">共享运营公司测评系统</span>
          </div>
        </div>
        <!--    打票页面的提示-->
        <div class="flex justifyContentSpaceBetween" style="margin-left: 40px; margin-top:20px;align-items: flex-end;" v-if="mode!='hz'">
          <div  style="display: flex" class="shuoMing">
            <span v-if="footerText">★</span>
            <div>
              <div v-for="item in footerTextArr">{{item}}</div>
            </div>
          </div>
          <div class="flex justifyContentSpaceBetween " style="margin-right:40px;">
            <span><img src="/img/logo.png" width="40" height="40"></span><span class="ml10 font28">共享运营公司测评系统</span>
          </div>
        </div>

      <div class="buttomBtn" v-if="!hideBtn" style="position: fixed;width: 100%;background: #fff;bottom: 30px;">
        <!--      <span class="nextBtn">-->
        <!--        <el-button  type="primary">返回</el-button>-->
        <!--      </span>-->
        <span slot="footer" class="dialog-footer" style="position: relative;z-index: 2">
<!--        <span slot="footer" class="dialog-footer" style="position: relative;z-index: 2">-->
          <el-button type="primary" @click="up()" v-if="mode!='hz'&&currentPage!=1" class="nextBtn">上一张选票</el-button>
<!--        <el-button type="primary" v-if="mode!='hz'&&(xpList.length==1||dqwz==0)&&ishaveLast" @click="lastTab" class="nextBtn">上一步</el-button>-->
<!--        <el-button type="primary" @click="up()" v-if="dqwz>0||mode=='hz'" class="nextBtn">{{mode=='hz'?'返回':'上一张选票'}}</el-button>-->
        <!-- <span v-if="xpList.length>=1&&dqwz<xpList.length&&mode!='hz'" style="float: left">评票：{{ dqwz + 1 }}/{{ xpList.length }}</span> -->
        <el-tooltip content="点击可查看所有测评对象及测评结果。" placement="top" effect="light" v-if="dqwz<xpList.length&&mode!='hz'">
          <el-button type="warning" @click="openHz()" class="nextBtn">测评结果查询</el-button>
        </el-tooltip>
        <el-button type="primary" v-if="mode=='hz'" @click="tiJiao()" class="nextBtn">提交测评</el-button>
<!--        <el-button type="primary" @click="next()" v-if="mode!='hz' && (dqwz+1) < xpList.length" class="nextBtn">下一张选票</el-button>-->
<!--          <el-button type="primary" v-if="mode!='hz'&&xpList.length==1||(dqwz+1) ==xpList.length" @click="openHz" class="nextBtn">下一步</el-button>-->
  <el-button type="primary" v-if="mode!='hz'" @click="nextTab" class="nextBtn">下一张选票</el-button>
<!--          <span v-if="mode!='hz'" class="ml30 yeMa">-->
<!--             {{dqwz+1}} / {{xpList.length}}-->
<!--            </span>-->
      </span>
        <span v-if="mode!='hz'" class="yeMa">
           <span class="blue"> 当前第 {{currentPage}} 页</span> / 共 {{allPageData.length}} 页
<!--             <span class="blue"> 当前第 {{dqwz+1}} 页</span> / 共 {{xpList.length}} 页-->
            </span>
      </div>
      </div>
<!--    </el-dialog>-->
  </div>
</template>

<script>
import { tiJiaoHd, tiJiaoXpCode} from "@/api/ceping/cepinghdglxpxxb";
import {
  wdXuanPiaoXpNew,
  getMySheet,
  currentPageSave,
  wdXuanPiaoSm,
  daFenCpAnswer,
  xpInfo
} from "@/api/ceping/jxevalvote";
import {onlineRegister,onlineVerification} from "@/api/admin/online";
import cpXp3 from "./cpIndexXp3";
import cpXp4 from "./cpIndexXp4";
import cpXp5 from "./cpIndexXp5";
import cpHzPz from "./cpIndexHzPz";
import store from "@/store";

export default {
  name: "cpIndex.vue",
  components: {
    cpXp3,
    cpXp4,
    cpXp5,
    cpHzPz
  },
  data() {
    return {
      type: "",
      hdId: null,
      xcCode: "",
      dialogVisible: false,
      xpList: [],//所有选票
      dqwz: 0,//当前位置
      mode: "df",//df或hz
      timer: null,
      sheet:{
        sheetType:0
      },
      cepingpiaoVisible3:false,
      cepingpiaoVisible4:false,
      cepingpiaoVisible5:true,
      ishaveLast:false,
      allPageData:[],
      doneList:[],
      currentPage:0,
      footerText:'',
      userId:'',
      hideBtn:true,
      footerTextArr:[],
      logoHeight:200,
      msgList: [],
      qpItemId:"",
    }
  },
  beforeDestroy() {
    clearInterval(this.timer);
  },
  methods: {
    init() {
      this.xpList = [];
      this.dqwz = 0;
      this.mode = "df";
    },
    cacel() {
      //退出全屏
      function exitFullscreen() {
        if (document.exitFullscreen) {
          document.exitFullscreen();
        } else if (document.msExitFullscreen) {
          document.msExitFullscreen();
        } else if (document.mozCancelFullScreen) {
          document.mozCancelFullScreen();
        } else if (document.webkitExitFullscreen) {
          document.webkitExitFullscreen();
        }
      }

      clearInterval(this.timer);
      exitFullscreen();
      this.$emit('next');
    },
    //初始按会议
    initHd(dataObj) {
      this.init();
      this.type = "HD";
      this.hdId = dataObj.hdId;
      this.xcCode = dataObj.xpCode;
      this.isNotLastTab=dataObj.isNotLastTab;
      this.sheetType=dataObj.sheetType;
      this.allPageData=dataObj.allPageData;
      this.doneList=dataObj.doneList;
      this.currentPage=dataObj.currentPage;
      this.userId=this.$store.state.user.userInfo.userId;
      const params={
        hdId:this.hdId,
        userId:this.userId
      }
      wdXuanPiaoSm(params).then(res => {
        if (res.data.data.length > 0) {
          function fullScreen() {
            var element = document.documentElement;
            if (element.requestFullscreen) {
              element.requestFullscreen();
            } else if (element.msRequestFullscreen) {
              element.msRequestFullscreen();
            } else if (element.mozRequestFullScreen) {
              element.mozRequestFullScreen();
            } else if (element.webkitRequestFullscreen) {
              element.webkitRequestFullscreen();
            }
          }

          fullScreen();
          this.dialogVisible = true;
          this.xpList = res.data.data;
          //this.open(0)
          this.openXp(dataObj.currentPageObj.cppId)
        } else {
          this.$notify.closeAll();
          this.$notify.error('会议中没有选票信息')
        }
      })
    },
    //从汇总页面来的实名
    openHzToHd(dataObj){
      this.init();
      this.type = "HD";
      this.xcCode=dataObj.xcCode;
      this.hdId=dataObj.hdId;
      this.allPageData=dataObj.allPageData;
      this.doneList=dataObj.doneList;
      this.currentPage=dataObj.currentPage;
      this.xcCode=dataObj.xcCode;
      this.userId=this.$store.state.user.userInfo.userId;
      const params={
        hdId:this.hdId,
        userId:this.userId
      }
      wdXuanPiaoSm(params).then(res => {
        this.xpList = res.data.data;
        this.openXp(dataObj.currentPageObj.cppId)
      })
    },
    //初始按选票
    initXp(dataObj) {
      onlineVerification(dataObj.xpCode).then(ress=>{
        //票5登录后。文本输入框
        /*console.log('票5')
        console.log('onlineVerification返回值',ress)
        console.log('dataObj值',dataObj)*/
        if(ress.data.data=="false"||dataObj.ishaveLast){
          this.init();
          this.type = "XP";
          this.xcCode = dataObj.xpCode;
          this.isNotLastTab=dataObj.isNotLastTab;
          this.sheetType=dataObj.sheetType;
          this.allPageData=dataObj.allPageData;
          this.doneList=dataObj.doneList;
          this.currentPage=dataObj.currentPage;
          var params={
            xpCode:this.xcCode,
            sheetType:5
          }
          wdXuanPiaoXpNew(params).then(res => {
            if (res.data.data.length > 0) {
              function fullScreen() {
                var element = document.documentElement;
                if (element.requestFullscreen) {
                  element.requestFullscreen();
                } else if (element.msRequestFullscreen) {
                  element.msRequestFullscreen();
                } else if (element.mozRequestFullScreen) {
                  element.mozRequestFullScreen();
                } else if (element.webkitRequestFullscreen) {
                  element.webkitRequestFullscreen();
                }
              }
              this.timer = setInterval(()=>{
                onlineRegister(this.xcCode).then(resss=>{});
              }, 30000);
              fullScreen();
              this.dialogVisible = true;
              this.xpList = res.data.data;
              //再判断新票类型，是3-5那种票
              // Promise.all([
              //   getMySheet(this.xpList[0].cppId).then(reses => {
              //     this.sheet = reses.data.data;
              //   })
              // ]).then(res => {
              //   /* 你的逻辑代码 */
              //   this.open(0)
              // })
              this.openXp(dataObj.currentPageObj.cppId)
              //this.open(0)
              // this.$nextTick(() => {
              //   this.open(0)
              // });
            } else {
              this.$notify.closeAll();
              this.$notify.error('票号中没有选票信息');
            }
          })
        }else{
          this.$notify.closeAll();
          this.$notify.error('票号正在打分，不可重复上线');
        }
      })
    },
    //判断是否测评完成
    // yzDf(){
    //   let yzDf = 0;
    //   yzDf=this.$refs.cpXp5.yzDf();
    //   return yzDf;
    // },
    up() {
      if (this.mode == "hz") {
        this.mode = "df";
        //this.dqwz = this.xpList.length - 1;
        this.open(this.dqwz)
      } else {
        let yzDf = this.$refs.cpXp5.yzDf();
        if (yzDf > 0) {
          this.$confirm('当前页还有“' + (yzDf == 1 ? '指标项' : '评价等次') + '”没有测评，请继续完成测评。', '提示', {
            confirmButtonText: '执行操作',
            showConfirmButton: false,
            cancelButtonText: '继续打分',
            type: 'warning'
          }).then(() => {
            this.currentPageSave('upTab')
            // if (this.dqwz == 0) {
            //   this.mode = "hz";
            //   this.openHz()
            // } else {
            //   this.dqwz = this.dqwz - 1;
            //   this.open(this.dqwz)
            // }
          }).catch(() => {
          })
        } else {
          this.currentPageSave('upTab')
          // if (this.dqwz == 0) {
          //   this.mode = "hz";
          //   this.openHz()
          // } else {
          //   this.dqwz = this.dqwz - 1;
          //   this.open(this.dqwz)
          // }
        }
      }
    },
    //下一个步骤
    nextTab() {
      // this.$emit('nextTab', 'ceping4');
      this.next()
    },
    next() {
      if (this.mode == "hz") {
        this.mode = "df";
        this.dqwz = 0;
        this.open(this.dqwz)
      } else {
        // let yzDf = this.$refs.cpXp.yzDf();
        let yzDf = 0;
        yzDf = this.$refs.cpXp5.yzDf()
        if (yzDf > 0) {
          this.$confirm('当前页还有“' + '指标项' + '”没有测评，请继续完成测评。', '提示', {
            confirmButtonText: '执行操作',
            showConfirmButton: false,
            cancelButtonText: '继续测评',
            type: 'warning'
          }).then(() => {
            this.currentPageSave('nextTab')
            // if (this.dqwz == this.xpList.length - 1) {
            //   this.currentPageSave('nextTab')
            //   //this.$emit('toHuiZong','ceping5');
            //   // this.mode = "hz";
            //   // this.openHz()
            // } else {
            //   this.currentPageSave('nextPage')
            //   this.dqwz = this.dqwz + 1;
            //   this.open(this.dqwz)
            // }
          }).catch(() => {
          })
        } else {
          let shows = true;
          this.$refs.cpXp5.qList.forEach(item=>{
            if(item.answer == ''){
              shows = false;
              return
            }
          })
          if(shows){
            this.currentPageSave('nextTab')
          }else{
            this.$confirm('“票面有未填写选项，该选项是否按弃权处理?”', '提示', {
            confirmButtonText: '是',
            cancelButtonText: '否',
            type: 'warning'
          }).then(() => {
            this.currentPageSave('qpnextTab')
          }).catch(() => {
          })
          }
        }
      }
    },
    //上一个步骤
    lastTab(){
      this.$emit('lastTab','ceping4');
    },
    //从汇总页来的
    openHzToXp(dataObj){
      let params = {
        xpCode:dataObj.xcCode,
        sheetType:5
      }
      this.type = "XP";
      this.xcCode=dataObj.xcCode;
      this.allPageData=dataObj.allPageData;
      this.doneList=dataObj.doneList;
      this.currentPage=dataObj.currentPage;
      wdXuanPiaoXpNew(params).then(res => {
        this.xpList = res.data.data;
        this.openXp(dataObj.currentPageObj.cppId)
      })
    },

    openXp(id) {
      if(id){
        var findIndex=this.xpList.findIndex(item=>item.cppId==id)
        if(findIndex==-1){
          this.hideBtn=true;
        }else{
          this.hideBtn=false;
          this.dqwz = findIndex * 1;
          this.open(this.dqwz);
        }
        // for (var i in this.xpList) {
        //   if (this.xpList[i].cppId == id) {
        //     this.hideBtn=false;
        //     this.dqwz = i * 1;
        //     this.open(this.dqwz);
        //   }else{
        //     this.hideBtn=true
        //   }
        // }
      }else{
        this.open(0)
      }
    },
    open(i) {
      this.mode = "df";
      this.hdId = this.xpList[i].hdId;
      this.$nextTick(()=>{
        if(this.type=='HD'){
          this.$refs.cpXp5.initHd(this.hdId,this.xpList[i].cppId,this.userId);
        }else{
          this.$refs.cpXp5.initXp(this.xpList[i].cppId,this.xcCode);
        }
        setTimeout(() => {
          this.footerText= this.$refs.cpXp5.getFooterText()
          if(this.footerText){
            this.footerTextArr=[];
            this.footerText=this.footerText.replace(/(.{50})/g,'$1\n')
            var footerTextArr=this.footerText.split("\n");
            footerTextArr.forEach(item=>{
              if(item){
                this.footerTextArr.push(item);
              }
            })
            this.logoHeight=this.footerTextArr.length>1?190+(this.footerTextArr.length-1)*35:200;
          }
        }, 1000);
      })
    },
    openHz() {
      if (this.mode == "hz") {
        this.currentPageSave('toHuiZong')
        //this.$emit('toHuiZong','ceping5');
        // this.$nextTick(() => {
        //   if (this.type == "HD") {
        //     this.$refs.cpHz.initHd(this.hdId)
        //   } else {
        //     this.$refs.cpHz.initXp(this.xcCode)
        //   }
        // })
        //return;
      }
      let yzDf =0;
      yzDf = this.$refs.cpXp5.yzDf()
      if (yzDf > 0) {
        this.$confirm('当前页还有“' + (yzDf == 1 ? '指标项' : '评价等次') + '”没有测评，请继续完成测评。', '提示', {
          confirmButtonText: '执行操作',
          showConfirmButton: false,
          cancelButtonText: '继续打分',
          type: 'warning'
        }).then(() => {
          this.currentPageSave('toHuiZong')
          //this.mode = "hz";
          //this.$emit('toHuiZong','ceping5');
          this.$nextTick(() => {
            // if (this.type == "HD") {
            //   this.$refs.cpHz.initHd(this.hdId)
            // } else {
            //   this.$refs.cpHz.initXp(this.xcCode)
            // }
          })
        }).catch(() => {
        })
      } else {
        this.currentPageSave('toHuiZong')
        //this.mode = "hz";
        //this.$emit('toHuiZong','ceping5');
        // this.$nextTick(() => {
        //   if (this.type == "HD") {
        //     this.$refs.cpHz.initHd(this.hdId)
        //   } else {
        //     this.$refs.cpHz.initXp(this.xcCode)
        //   }
        // })
      }
    },
    tiJiao() {
      this.$confirm('正式提交后将不可修改，是否确认提交测评？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        if (this.type == "HD") {
          tiJiaoHd(this.hdId).then(res => {
            if (res.data.code == "0") {
              this.$notify.success("此次测评会议已提交成功");
              this.cacel();
              this.dialogVisible = false;
            } else {
              this.$notify.closeAll();
              this.$notify.error(res.data.msg)
            }
          })
        } else {
          tiJiaoXpCode(this.xpList[0].hdId, this.xcCode).then(res => {
            if (res.data.code == "0") {
              this.$notify.success("此次测评会议已提交成功");
              this.dialogVisible = false;
              this.cacel();
            } else {
              this.$notify.closeAll();
              this.$notify.error(res.data.msg)
            }
          })
        }
      })
    },
    handleClose(done) {
      this.$confirm('您的测评还没有完成，请继续完成测评。', "提示", {
        confirmButtonText: '执行操作',
        showConfirmButton: false,
        cancelButtonText: '继续打分',
        type: 'warning'
      }).then(_ => {
        done();
      }).catch(_ => {
      });
    },
    getModeEvent(data){
      this.mode = data;
    },
    //去汇总页之前需要先保存数据
    currentPageSave(toPage){

      let xcCodeX = this.xcCode
      const param={
        voteId:0,
        sheetType:5,
        xpCode:xcCodeX,
        cppId:this.xpList[this.dqwz].cppId,
        isSm:this.type=='HD'?1:0,
        hdId:this.hdId,
        userId:this.$store.state.user.userInfo.userId
      }
      console.log(param)
      if(toPage == "qpnextTab"){
        const params ={
          cppId:this.xpList[this.dqwz].cppId,
          xpCode:xcCodeX,
        }
        console.log(params)
        xpInfo(params).then(res => {
          this.msgList = JSON.parse(res.data.data.cepingHdglCppmxbInfo.ruleDetailJson);

          this.msgList.map(it =>{
            this.qpItemId = it.itemId;
          })
          let answerJson = [];
          var obj ={
            itemId:this.qpItemId,
            answer:'',
          }

          answerJson.push(obj)
          const paramss ={
            hdId:this.hdId,
            cppId:this.xpList[this.dqwz].cppId,
            xpCode:xcCodeX,
            answerJson:answerJson,
            userId:0
          }
          console.log(paramss);
          daFenCpAnswer(paramss).then((response)=>{
            if(response.data.code == 0){
              console.log("弃票保存成功")
              // this.$message.success( '提交成功!')
            }
          })
        })



        currentPageSave(param).then(res=>{
          if(res.data.code==0){
            clearInterval(this.timer);
            if(toPage=='toHuiZong'){
              var params={
                sheetType:5,
                clickTab:'ceping5',
                xpid:this.xpList[this.dqwz].id,
                cppId:this.xpList[this.dqwz].cppId
              }
              this.$emit('toHuiZong',params);
            }else if(toPage=='qpnextTab'){
              var params={
                toPage:'ceping5',
                tpXpId:this.xpList[this.dqwz].id,
                cppId:this.xpList[this.dqwz].cppId
              }
              this.$emit('nextTab',params);
              //this.$emit('nextTab','ceping5');
            }else if(toPage=='upTab'){
              this.$emit('lastTab','ceping5');
            }
          }
        })
      }else{
        currentPageSave(param).then(res=>{
          if(res.data.code==0){
            clearInterval(this.timer);
            if(toPage=='toHuiZong'){
              var params={
                sheetType:5,
                clickTab:'ceping5',
                xpid:this.xpList[this.dqwz].id,
                cppId:this.xpList[this.dqwz].cppId
              }
              this.$emit('toHuiZong',params);
            }else if(toPage=='nextTab'){
              var params={
                toPage:'ceping5',
                tpXpId:this.xpList[this.dqwz].id,
                cppId:this.xpList[this.dqwz].cppId
              }
              this.$emit('nextTab',params);
              //this.$emit('nextTab','ceping5');
            }else if(toPage=='upTab'){
              this.$emit('lastTab','ceping5');
            }
          }
        })
      }

    },
  }
}
</script>

<style scoped>
</style>
